//
//  DeliveryAddressTableViewCell.m
//  xpkc
//
//  Created by Tiyon on 15/12/2.
//  Copyright © 2015年 TopSage. All rights reserved.
//

#import "DeliveryAddressTableViewCell.h"

#import "UserMessage.h"

#import "NewAddressViewController.h"

@interface DeliveryAddressTableViewCell()
{
    
    DeliveryAddressModel * _deliveryModel;
    
}
@end

@implementation DeliveryAddressTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        [self createUI];
        
    }
    
    return self;
    
}

-(void)createUI{
    
    CGFloat userNameH = 20;
    
    CGFloat offwidth = 5;// 0.04 * SCREEN_WIDTH;
    self.username = [[UILabel alloc] initWithFrame:CGRectMake(offwidth * 2 , userNameH, 140, 20)];
    self.username.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.username];
    
    
    self.phoneView = [[UIImageView alloc] initWithFrame:CGRectMake(self.username.frame.origin.y + 140, userNameH, 13, 20)];
    [self.phoneView setImage:[UIImage imageNamed:@"phone"]];
    [self.contentView addSubview:self.phoneView];
    
    
    
    
    self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.phoneView.frame.origin.x + 13, userNameH, SCREEN_WIDTH - self.username.frame.origin.x - 140-10 - 13 , 20)];
    self.phoneLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.phoneLabel];
    
    //    self.zipLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, userNameH, SCREEN_WIDTH  - 290, 20)];
    //
    //    [self.contentView addSubview:self.zipLabel];
    
    //    self.userLocationView = [[UIImageView alloc] initWithFrame:CGRectMake(20, userNameH + 30, 20, 20)];
    //    [self.userLocationView setImage:[UIImage imageNamed:@""]];
    //    [self.contentView addSubview:self.userLocationView];
    
    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(offwidth * 2, userNameH + 20 + offwidth, SCREEN_WIDTH - 50, 40)];
    self.addressLabel.numberOfLines = 0;
    
    self.addressLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.addressLabel];
    
    
    
    
    
    self.editBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, userNameH + 70+offwidth, 80, 30)];
    self.editBtn.backgroundColor = OrangeColor;
    [self.editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [self.editBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.editBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.editBtn];
    
    [self.editBtn addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
    self.editBtn.layer.cornerRadius = 10;
    self.editBtn.layer.masksToBounds = YES;
    self.deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 0.5 + 10, userNameH + 70 + offwidth, 80, 30)];
    
    self.deleteBtn.backgroundColor = OrangeColor;
    [self.deleteBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [self.deleteBtn addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
    self.deleteBtn.layer.cornerRadius = 10;
    self.deleteBtn.layer.masksToBounds = YES;
    self.deleteBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [self.contentView addSubview:self.deleteBtn];
    
    
}

//-(void)delete{
//    
//       NSString *module = @"address";
//    NSString *interface = @"deladdress";
//    
//    NSString *timeString = [GetTime getTime];
//    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
//    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
//    
//    NSDictionary *parameter = @{@"time":timeString,@"mid":[UserMessage sharedUserMessage].userMid,@"id":_deliveryModel.codeId};
//    
//    
//    NSLog(@"URLSTRING--%@",urlString);
//    NSLog(@"parameter--%@--%@",parameter,_deliveryModel.address);
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
//        logdebug(@"%@",dic);
//        
//        if ([dic objectForKey:@"succ"]) {
//            
//            self.myblock(@"delete");
//        }
//        
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        //
//    }];
//
//}


-(void)edit{
    
    NewAddressViewController * newaddress = [[NewAddressViewController alloc] init];
    
    newaddress.deliverymodel = _deliveryModel;
    [self.viewController.navigationController pushViewController:newaddress animated:YES];
    
}

-(void)configCellWithDeliveryModel:(DeliveryAddressModel *)model{
    
    
    _deliveryModel = model;
    self.username.text = [NSString stringWithFormat:@"收货人：%@",model.consignee];
    
    self.phoneLabel.text = model.tel;
    NSLog(@"%@",model.address);
    self.addressLabel.text = [NSString stringWithFormat:@"地址：%@%@",model.address,model.des];
    
    self.zipLabel.text = [NSString stringWithFormat:@"邮编：%@",model.zip];
    
    
    
    
}

@end
